package com.mywang.weitravel.modules.redlocate;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.adapter.RedLocateListAdapter;
import com.mywang.weitravel.base.BaseFragment;
import com.mywang.weitravel.model.RedLocateListBean;

import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * Created by wangmingyun on 2019/12/11.
 */
public class RedFragment extends BaseFragment<RedLocatePresenter> implements RedLocateContract.View, RedLocateListAdapter.RedMarkListener {

    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private RedLocateListAdapter listAdapter;

    @Override
    protected RedLocatePresenter initPresenter() {
        return new RedLocatePresenter(this);
    }

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_red_look;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        listAdapter = new RedLocateListAdapter(mActivity, this);
        rvList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvList.setAdapter(listAdapter);
        rvList.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                Jzvd jzvd = view.findViewById(R.id.videoplayer);
                if (jzvd != null && Jzvd.CURRENT_JZVD != null &&
                        jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        Jzvd.releaseAllVideos();
                    }
                }
            }
        });
        mPresenter.requestData("1");
    }

    @Override
    public void onSuccess(List<RedLocateListBean> listBeans) {
        listAdapter.setmData(listBeans);
    }

    @Override
    public void markRedSuccess(int position) {
        listAdapter.updateItem(position);
    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onDestroy() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onDestroy();
    }

    @Override
    public void markRed(int position, String id) {
        mPresenter.markRed(position, id);
    }
}
